package com.ruoyi.TimedTask.controller;
import com.ruoyi.TimedTask.domain.TimedTask;
import com.ruoyi.TimedTask.service.TimedTaskService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.config.properties.ExportDbProperties;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 定时任务Controller
 *
 * @author zjh
 * @date 2022-05-06
 */
@Controller
@RequestMapping("/task")
public class TimedTaskController extends BaseController {
    private String prefix = "timedTask";

    @Autowired
    private TimedTaskService timedTaskService;

    @Autowired
    private ExportDbProperties exportDbProperties;

    @RequiresPermissions("business:task:view")
    @GetMapping()
    public String task() {
        return prefix + "/timedTask";
    }

    /**
     * 查询定时任务列表
     */
    @RequiresPermissions("business:task:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "beginTime", required = false) String beginTime, @RequestParam(value = "endTime", required = false) String endTime) {
        startPage();
        TimedTask timedTask = new TimedTask();
        timedTask.setBeginTime(beginTime);
        timedTask.setEndTime(endTime);
        Long companyId = SecurityUtils.getLoginUser().getCompanyId();
        timedTask.setCompanyId(companyId);
        List<TimedTask> list = timedTaskService.selectTimedTaskList(timedTask);
        return getDataTable(list);
    }

    /**
     * 导出定时任务列表
     */
    @RequiresPermissions("business:task:export")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(TimedTask timedTask, HttpServletResponse response) {
        timedTask.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
        List<TimedTask> list = timedTaskService.selectTimedTaskList(timedTask);
        ExcelUtil<TimedTask> util = new ExcelUtil<TimedTask>(TimedTask.class);
        util.exportExcel(response, list, "温度湿度记录");

    }

    /**
     * 修改定时任务
     */
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap) {
        TimedTask timedTask = timedTaskService.selectTimedTaskById(taskId);
        mmap.put("timedTask", timedTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存定时任务
     */
    @RequiresPermissions("business:task:edit")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody TimedTask timedTask) {
        return toAjax(timedTaskService.updateTimedTask(timedTask));
    }

    /**
     * 删除定时任务
     */
    @RequiresPermissions("business:task:remove")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @GetMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestParam(value = "taskId", required = false) String taskId) {
        return toAjax(timedTaskService.deleteTimedTaskByIds(taskId));
    }

}
